#include "Cmaid_Gtest.h"

#define ADD_COUNT 130

TEST_P(Cmaid_Cache_Gtest, instance)
{
	int i;
	void *string;

	string = cmaid_cache_instance(cache, dataAt(0));

	ASSERT_TRUE(NULL != string);
	ASSERT_DATA_EQ(dataAt(0), string);
	if (isPointerType())
		ASSERT_EQ(dataAt(0), string);
	else
		ASSERT_NE(dataAt(0), string);

	for (i = 0; i < ADD_COUNT; i++)
	{
		void *ret;
		ret = cmaid_cache_instance(cache, dataAt(0));

		ASSERT_TRUE(NULL != ret);
		ASSERT_DATA_EQ(dataAt(0), ret);
		ASSERT_EQ(string, ret);
	}
}

TEST_P(Cmaid_Cache_Gtest, instance_release)
{
	int i;
	void *string;

	string = cmaid_cache_instance(cache, dataAt(0));

	ASSERT_TRUE(NULL != string);
	ASSERT_DATA_EQ(dataAt(0), string);
	if (isPointerType())
		ASSERT_EQ(dataAt(0), string);
	else
		ASSERT_NE(dataAt(0), string);

	for (i = 0; i < ADD_COUNT; i++)
	{
		void *ret;
		ret = cmaid_cache_instance(cache, dataAt(0));

		ASSERT_TRUE(NULL != ret);
		ASSERT_DATA_EQ(dataAt(0), ret);
		ASSERT_EQ(string, ret);
		if (i % 2)
			cmaid_cache_release(cache, ret);
	}
}

TEST_P(Cmaid_Cache_Gtest, release)
{
	int i;
	void *string;
	Cmaid_Iter it;

	for (i = 0; i < ADD_COUNT; i++)
	{
		string = cmaid_cache_instance(cache, dataAt(0));

		ASSERT_TRUE(NULL != string);
		ASSERT_DATA_EQ(dataAt(0), string);
		ASSERT_EQ(1, cmaid_container_count(cache));
	}
	
	for (i = 0; i < ADD_COUNT; i++)
		cmaid_cache_release(cache, string);

	ASSERT_EQ(0, cmaid_container_count(cache));
	/* and now see if it is really empty */
	cmaid_container_iter_attach(cache, &it);
	string = cmaid_iter_next(&it);
	ASSERT_TRUE(NULL == string);
	cmaid_container_iter_detach(cache, &it);
}

TEST_P(Cmaid_Cache_Gtest, many_add)
{
	int i;
	void *string;
	Cmaid_Iter it;

	for (i = 0; i < dataCount(); i++)
	{
		string = cmaid_cache_instance(cache, dataAt(i));

		ASSERT_TRUE(NULL != string);
		ASSERT_DATA_EQ(dataAt(i), string);
		ASSERT_EQ(i + 1, cmaid_container_count(cache));
		if (isPointerType())
			ASSERT_EQ(dataAt(i), string);
		else
			ASSERT_NE(dataAt(i), string);
	}

	cmaid_container_clear(cache);
	ASSERT_EQ(0, cmaid_container_count(cache));	
	/* and now see if it is really empty */
	cmaid_container_iter_attach(cache, &it);
	string = cmaid_iter_next(&it);
	ASSERT_TRUE(NULL == string);
	cmaid_container_iter_detach(cache, &it);
}
/* disable this for now because it is segafaulting, cmaid cache needs definately
 * some more unit tests! */
#if 0
TEST_P(Cmaid_Cache_Gtest, dump)
{
	Cmaid_Iter iter __CMAID_CHECKED_ITER__;
	FILE *file;
	void *data;
	int size;

	/* Of course we cannot test the output of dump here without
	 * writing an disproportional difficult parser. So we only test
	 * it to see if it segfault and do some alibi tests */
	fill();
	cmaid_container_iter_attach(cache, &iter);
	data = cmaid_iter_index_goto(&iter, 3);
	size = cmaid_container_count(cache);

	file = tmpfile();
	cmaid_container_dump(cache, file);
	fclose(file);

	ASSERT_EQ(size, cmaid_container_count(cache));
	ASSERT_DATA_EQ(data, cmaid_iter_current(&iter));
	ASSERT_EQ(3, cmaid_iter_index_get(&iter));
	
	cmaid_container_iter_detach(cache, &iter);
}
#endif

